<?php 
echo validation_errors();
$form = array(
    'id'    => 'form-game',
    'class' => 'span8 ut-form no-gutter row'
);
$options = array(
    'postponed' => 'Przełożony',
    'played'    => 'Rozegrany'
);
echo form_open('league/update_game',$form);
echo form_fieldset();
echo '<ul class="span4">'
        .'<li><h3>Kolejka numer: '.$game['game_fixture'].' - '.date('d.m.Y',$game['game_date']).', godz. '.$game['game_hour'].'</h3></li>'
        .'<li>'.form_label('Stan meczu','state',array('class'=>'span1')).form_dropdown('state',$options,'played','class="span1"').'</li>'
        .'<li>'
            .'<img class="thumbnail left" src="'.((empty($game['gosp_logo'])) ? place_img(75,75,'dddddd','dddddd') : base_url($this->config->item('image_team_path').$game['gosp_logo'])).'" alt="Logo '.$game['gosp_name'].'" width="75" height="75"/>'
            .form_label($game['gosp_code'],'gosp_for',array('class'=>'span1'))
            .form_input('gosp_for',set_value('gosp_for',$game['gosp_for']),'class="micro" maxlength="2"')
        .'</li>'
        .'<li>'
            .'<img class="thumbnail left" src="'.((empty($game['gosc_logo'])) ? place_img(75,75,'dddddd','dddddd') : base_url($this->config->item('image_team_path').$game['gosc_logo'])).'" alt="Logo '.$game['gosc_name'].'" width="75" height="75"/>'
            .form_label($game['gosc_code'],'gosc_for',array('class'=>'span1'))
            .form_input('gosc_for',set_value('gosc_for',$game['gosc_for']),'class="micro" maxlength="2"')
        .'</li>'
    .'</ul>';
echo '<div class="span4">'
        .'<p>'
            .anchor('#','Dodaj zdarzenie',array('class'=>'btn btn-mini btn-success','id'=>'add-event'))
        .'</p>'
        .'<table class="no-gutter table table-striped table-condensed" id="events">'
            .'<thead>'
                .'<tr>'
                    .'<th>Zawodnik</th>'
                    .'<th>Zdarzenie</th>'
                    .'<th>Ilość</th>'
                    .'<td></td>'
                .'</tr>'
            .'</thead>'
            .'<tbody>'
            .'</tbody>'
        .'</table>'
    .'</div>';
echo form_fieldset_close();
echo '<p>'
        .form_hidden('gid',$game['game_id'])
        .form_hidden('t1id',$game['gosp_id'])
        .form_hidden('t2id',$game['gosc_id'])
        .form_hidden('lid',$game['game_league'])
        .form_submit('edit_game','Zapisz zmiany','class="btn btn-primary btn-large"')
    .'</p>';
echo form_close();
?>
<script>
$(function(){   
    $('#add-event').on('click',function(e){
        e.preventDefault();
        var events,players;
 
        $.ajax({
            type: 'GET',
            async: false,
            url: '../get_json_events',
            dataType: 'json',
            success: function(data){
                events = '<select name="event[]" class="small">';
                $.each(data,function(k,v){
                    events += '<option value="'+k+'">'+v.name+'</option>';
                });
                events += '</select>';
            }
        });

        $.ajax({
            type: 'GET',
            async: false,
            url: '../get_json_team_players/'+<?php echo $game['gosp_id']; ?>+'/'+<?php echo $game['gosc_id']?>,
            dataType: 'json',
            success: function(data){
                if(data!=''){
                    players = '<select class="medium" name="player[]">';
                    $.each(data,function(k,v){
                        players += '<optgroup label="'+k+'">';
                        $.each(v,function(x,y){
                            players += '<option value="'+x+'">'+y+'</option>';
                        });
                    });
                    players += '</select>';
                }
                else
                    players = '';
            }
        });
            
        var idx = $('#events tbody tr').length;
        $('#events tbody').append('<tr><td>'+players+'</td><td>'+events+'</td><td><input type="text" name="value[]" class="micro" maxlength="2"/></td><td><a href="#" class="btn btn-mini btn-danger btn-delete" id="delete-event-'+idx+'">Usuń</a>&nbsp;<a href="#" id="save-event-'+idx+'" class="btn btn-mini btn-success btn-save">Zapisz</a></td></tr>');
    });
    
    $('#events').on('click','a.btn-save',function(e){
        e.preventDefault();
        var id = $(this).prop('id');
        console.log('ID: '+id); 
    });
});
</script>